package com.casic.common.base.service.impl;

import com.casic.common.base.dao.BaseDao;
import com.casic.common.base.entity.BaseEntity;
import com.casic.common.base.entity.PageEntity;
import com.casic.common.base.service.BaseService;
import com.casic.common.utils.Constant;
import com.casic.modules.sys.entity.SysUserEntity;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.SecurityUtils;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

public abstract class BaseServiceImpl<baseDao extends BaseDao<T>, T extends BaseEntity>  implements BaseService<T> {

    protected  baseDao dao;


    /** 生成主键策略 */
    public String createId() {
        return UUID.randomUUID().toString().replaceAll("\\-", "");
    }

    @Override
    @Transactional
    public T save(T entity) {
        String id=createId();
        entity.setId(id);
        entity.setCreateDate(new Date());
        SysUserEntity sysUserEntity= (SysUserEntity)SecurityUtils.getSubject().getPrincipal();
        entity.setCreateUserId(sysUserEntity.getUserId()+"");
        entity.setDeleteState(Constant.DELETE_STATE_NO);
        dao.save(entity);
        return findById(id);
    }

    @Override
    public void saveEntity(T entity) {
        String id=createId();
        entity.setId(id);
        entity.setCreateDate(new Date());
        SysUserEntity sysUserEntity= (SysUserEntity)SecurityUtils.getSubject().getPrincipal();
        entity.setCreateUserId(sysUserEntity.getUserId()+"");
        entity.setDeleteState(Constant.DELETE_STATE_NO);
        dao.save(entity);
    }

    @Override
    @Transactional
    public T edit(T entity) {
        SysUserEntity sysUserEntity= (SysUserEntity)SecurityUtils.getSubject().getPrincipal();
        entity.setModifyUserId(sysUserEntity.getUserId()+"");
        entity.setModifyDate(new Date());
        dao.update(entity);
        return findById(entity.getId());
    }

    @Override
    public void remove(T entity) {
        entity.setDeleteState(Constant.DELETE_STATE_YES);
        SysUserEntity sysUserEntity= (SysUserEntity)SecurityUtils.getSubject().getPrincipal();
        entity.setModifyUserId(sysUserEntity.getUserId()+"");
        entity.setModifyDate(new Date());
        dao.deleteById(entity);
    }

    @Override
    public void removeByIds(String[] ids) {
        Map<String, Object> params=new HashMap<>();
        params.put("ids",ids);
        params.put("deleteState",Constant.DELETE_STATE_YES);
        params.put("modifyDate",new Date());
        SysUserEntity sysUserEntity= (SysUserEntity)SecurityUtils.getSubject().getPrincipal();
        params.put("modifyUserId",sysUserEntity.getUserId()+"");
        dao.deleteByIds(params);
    }

    @Override
    public T findById(String id) {
        return dao.queryObject(id);
    }

    @Override
    public PageEntity<T> findPage(Map<String, Object> params){
        int currentPage=1;
        int pageSize=10;
        if(params.get("page")!=null){
            currentPage=Integer.parseInt((String)params.get("page"));
        }
        if(params.get("limit")!=null){
            pageSize=Integer.parseInt((String)params.get("limit"));
        }
        params.put("deleteState",Constant.DELETE_STATE_NO);
        PageHelper.startPage(currentPage, pageSize);
        List<T> list=dao.queryListByPage(params);
        return new PageEntity<T>(list);
    }

    @Override
    public List<T> findList(Map<String, Object> params){
         return dao.queryListByPage(params);
    }
    @Override
    public List<T> saveBatch(List<T> list){
        List<T> data=new ArrayList<>();
        SysUserEntity sysUserEntity= (SysUserEntity)SecurityUtils.getSubject().getPrincipal();
        for(int i=0;i<list.size();i++){
            T entity=list.get(i);
            String id=createId();
            entity.setId(id);
            entity.setDeleteState(Constant.DELETE_STATE_NO);
            entity.setCreateDate(new Date());
            entity.setCreateUserId(sysUserEntity.getUserId()+"");
            data.add(entity);
        }
        dao.saveBatch(data);
        return data;
    }
}
